package com.fmi.cloud.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.fmi.cloud.R;
import com.fmi.cloud.util.StringUtil;

/**
 * 新的dialog样式,使用场景:更新和强制更新,及单按钮dialog
 * Created by HeYuCan on 2016/8/29.
 */
public class NewConfirmDialog extends Dialog {

    private Context context;
    private String title;
    private String message;
    private String buttonLeftText;
    private String buttonRightText;
    //(显示更新包大小),传入null时,则为单按钮的对话框,此时String buttonLeftText也应传入null,若需双按钮的对话框(不用于更新)请使用ConfirmDialog.java
    private String updateSize = "";

    private ClickListenerInterface clickListenerInterface;

    /**
     * 单按钮时的对话框
     */
    public NewConfirmDialog(Context context, String title, String message, String buttonRightText) {
        this(null, context, title, message, null, buttonRightText);
    }

    /**
     * @param updateSize 显示更新包大小;   若需双按钮的对话框(不用于更新)请使用ConfirmDialog.java
     */
    public NewConfirmDialog(String updateSize, Context context, String title, String message,
                            String buttonLeftText, String buttonRightText) {
        super(context, R.style.NewUIAlertViewStyle);

        this.updateSize = updateSize;
        this.context = context;
        this.title = title;
        this.message = message;
        this.buttonLeftText = buttonLeftText;
        this.buttonRightText = buttonRightText;
    }

    public interface ClickListenerInterface {
        public void doLeft();

        public void doRight();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        inite();
    }

    public void inite() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_layout2, null);
        setContentView(view);

        TextView tvMessage = (TextView) view.findViewById(R.id.tvMessage);
        TextView tvLeft = (TextView) view.findViewById(R.id.tvBtnLeft);
        TextView tvRight = (TextView) view.findViewById(R.id.tvBtnRight);
        TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        TextView tvUpdateSize = (TextView) view.findViewById(R.id.tvUpdateSize);
        if (StringUtil.isBlank(title)) {
            tvTitle.setText("提示");
        } else {
            tvTitle.setText(title);
        }
        if (updateSize == null) {
            tvUpdateSize.setVisibility(View.GONE);
        } else {
            tvUpdateSize.setText(String.format("更新包大小:%sMB", updateSize));
        }

        tvMessage.setText(message);
        if (buttonLeftText == null) {
            tvLeft.setVisibility(View.GONE);
        }
        if (buttonRightText == null) {
            tvRight.setVisibility(View.GONE);
        }
        tvLeft.setText(buttonLeftText);
        tvRight.setText(buttonRightText);

        tvLeft.setOnClickListener(new ClickListener());
        tvRight.setOnClickListener(new ClickListener());

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics();

        lp.width = (int) (d.widthPixels * 0.8);
        dialogWindow.setAttributes(lp);
    }

    public void setClicklistener(ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }

    private class ClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {

            int id = v.getId();
            switch (id) {
                case R.id.tvBtnLeft:
                    clickListenerInterface.doLeft();
                    break;
                case R.id.tvBtnRight:
                    clickListenerInterface.doRight();
                    break;

                default:
                    break;
            }
        }
    }
}
